package com.yaozheng.vocationaltraining.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.LogUtils;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 抽奖
 */
@EViewGroup(R.layout.view_my_lottery)
public class MyLotteryView extends LinearLayout {
    @ViewById
    ImageView lotteryBgImageView, lotteryButtonImageView;

    private String[] mStrsOfTop = new String[]{"谢谢", "3", "100", "5",
            "谢谢", "1", "20", "谢谢", "15", "1"};
    private int[] angles;

    private int winningSubscript;

    private boolean isStop;
    /**
     * 是否消耗
     */
    private boolean isDestroy;

    float angle;

    public MyLotteryView(Context context) {
        super(context);
        initView();
    }

    public MyLotteryView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public void initView() {
        isDestroy = false;

        angle = (float) (360 / mStrsOfTop.length);
        angles = new int[mStrsOfTop.length];
        for (int i = 0; i < mStrsOfTop.length; i++) {
            float from = 270 - (i + 1) * angle;
            float to = from + angle;
            angles[i] = (int) from;
        }
        isStop = true;
        winningSubscript = 0;
    }

    @Click(R.id.lotteryButtonImageView)
    public void lotteryButtonClick() {
        if (isStop) {
            isStop = false;
            startRotation();
            automaticStop();
        }
//        else {
//            lotteryButtonImageView.setImageResource(R.drawable.button_lottery);
//            isStop = true;
//        }
    }

    float rotation = 0;

    @UiThread
    public void rotation() {
        if (!isDestroy) {
            if (rotation > 360) {
                rotation = 0;
            }
            lotteryBgImageView.setRotation(rotation);
            float from = angles[winningSubscript];
            float to = angles[winningSubscript] + angle;
            if (isStop && rotation >= from && rotation <= to) {
                LogUtils.println(mStrsOfTop[winningSubscript]);
                return;
            } else {
                startRotation();

            }
            rotation += 5;
        }
    }

    @Background
    public void startRotation() {
        if (!isDestroy) {
            rotation();

        }
    }


    @Background
    public void automaticStop() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isStop = true;
    }

}
